<?php
function removeWhite($input){
    return str_replace(' ','%20',$input);
}
$urlPart1='';
$urlPart2='';
$key='&api_key=2e0f6a31b6598dae0585ac68964d579c';
if(isset($_POST['option'])){
    if($_POST['option']=='track'){
        //artist top 20 tracks
        $urlPart1='http://ws.audioscrobbler.com/2.0/?method=track.search&format=json';
        
        $urlPart2='&track='.removeWhite($_POST['query']);
        $url=$urlPart1.$key.$urlPart2;
        $html=file_get_contents($url,true);
        $json=json_decode($html,true);
        $out=array();
        foreach($json['results']['trackmatches']['track'] as $key=>$val){
            $out[$key]['track']=$val['name'];
            $out[$key]['artist']=$val['artist'];
        }
        print(json_encode($out));
    }
    else{
       //20 tracks
       $urlPart1='http://ws.audioscrobbler.com/2.0/?method=artist.gettoptracks&format=json';
       $urlPart2='&artist='.removeWhite($_POST['query']);
    
        $url=$urlPart1.$key.$urlPart2;
        $html=file_get_contents($url,true);
        $json=json_decode($html,true);
        $out=array();
        foreach($json['toptracks']['track'] as $key=>$val){
            $out[$key]['track']=$val['name'];
            $out[$key]['artist']=$_POST['query'];
        }
        print(json_encode($out));
    }
}
?>